from django.shortcuts import render,get_object_or_404,redirect
from django.urls import reverse
from django.views import View
from django.http import HttpResponse
from django.http.response import HttpResponseBadRequest,HttpResponseNotFound
from django_redis import get_redis_connection
from django.http import JsonResponse
from django.conf import settings
import re
from user.models import User
from home.models import ArticleCategory,Article,Comment
from django.core.paginator import Paginator,EmptyPage #分页器
# Create your views here.

# 首页显示
class HomeIndexView(View):

    def get(self,request):
        # 获取分类信息
        categories = ArticleCategory.objects.all()
        # 获取用户要看的分类的id
        cat_id = request.GET.get('cat_id',1)
        # 查询分类
        try:
            category = ArticleCategory.objects.get(id=cat_id)
        except ArticleCategory.DoesNotExist:    
            return HttpResponseBadRequest('查询分类失败')
        # 获取分页信息
        page_num = request.GET.get('page_num',1)
        page_size = request.GET.get('page_size',5)
        # 根据分类获取文章
        articles = Article.objects.filter(category=category)
        # 创建分页器
        paginator = Paginator(articles,per_page=page_size)
        # 进行分页处理
        try:
            page_articles = paginator.page(page_num)
        except EmptyPage:
            return HttpResponseNotFound('empty page')
        # 总页数
        total_page = paginator.num_pages
        # 组织上下文
        context = {
            'categories':categories,
            'category':category,
            'articles':page_articles,
            'page_num':page_num,
            'total_page':total_page,
            'page_size':page_size,
            }
        # 返回应答
        return render(request,'index.html',context=context)

class DetailView(View):
    def get(self,request):
        # 获取信息
        categories = ArticleCategory.objects.all()
        article_id = request.GET.get('id')
        # 获取用户要看的分类的id
        cat_id = request.GET.get('cat_id',1)
        # 查询分类
        try:
            category = ArticleCategory.objects.get(id=cat_id)
        except ArticleCategory.DoesNotExist:
            return HttpResponseBadRequest('查询分类失败')
        # 查询文章  使用新的函数 找得到对象返回对象 找不到返回404
        # article = get_object_or_404(Article,id=article_id)
        try:
            article = Article.objects.get(id=article_id)
            # 查询到就代表被访问了 所以访问量给他加一
            article.total_views += 1
            article.save()
        except Article.DoesNotExist:
            return render(request,'404.html')
        # 查询推荐 取前十
        recommend_article = Article.objects.exclude(id = article.id).order_by('-total_views')[:9]
        
        # 获取评论信息
        page_size = request.GET.get('page_size',5)
        page_num = request.GET.get('page_num',1)
        # 根据文章查询评论
        comments = Comment.objects.filter(article=article).order_by('-comment_time')
        # 评论总数
        comment_total = comments.count()
        # 创建分页器
        paginator = Paginator(comments,page_size)
        # 获取页面数据
        try:
            page_comments = paginator.page(page_num)
        except EmptyPage:
            return HttpResponseBadRequest('empty page')
        # 总页数
        total_page = paginator.num_pages

        # 组织上下文
        context = {
            'categories':categories, #页头文章分类
            'category':category, #文章分类
            'article':article, #文章
            'recommend_article':recommend_article, #推荐文章
            'comments':page_comments, #要展示的评论
            'total_page':total_page, #总页数
            'page_size':page_size, #每页多少个
            'comment_total':comment_total, #评论总数
            'page_num':page_num, #第几页
            }
        # 返回应答
        return render(request,'detail.html',context=context)

    def post(self, request):
        user = request.user
        # 因为用户没有登录的话页面上就没有提交按钮 所以这里不用验证用户是否登录了
        # 接收数据
        id = request.POST.get('id')
        content = request.POST.get('content')
        # 验证文章是否存在
        try:
            article = Article.objects.get(id=id)
        except Article.DoesNotExist:
            return HttpResponseNotFound('此文章不存在')
        # 将数据写入数据库
        Comment.objects.create(
            content = content,
            article = article,
            user = user,
        )
        # 增加文章评论数
        article.comment_num += 1
        article.save()
        # 刷新页面 重定向
        path = reverse('home:detail') + '?id=%d'%article.id
        return redirect(path)

# 404
def page_not_found(request, exception):
    return render(request, '404.html')